package icu.twtool.generator

import icu.twtool.entity.BinaryEquation
import icu.twtool.entity.BinaryEquationBase
import icu.twtool.entity.Exercise

/**
 * 习题生成器
 * @param exerciseCount 习题的算式数量(default: 50)
 * @param equationGenerator 算式生成器(default: [EquationGenerator] 的默认构造器)
 */
class ExerciseGenerator(
    private val exerciseCount: Int = 50,
    private val equationGenerator: EquationGenerator = EquationGenerator()
) {

    /**
     * 生成一套习题
     * @param generateEquation 返回一个算式的方法
     */
    private fun generateExercise(count: Int = this.exerciseCount, generateEquation: () -> BinaryEquation): Exercise {
        val exercise = Exercise()
        while (exercise.size < count) {
            exercise.add(generateEquation())
        }
        return exercise
    }

    /**
     * 生成全是加法的习题
     */
    fun generateOfAdditions(count: Int = this.exerciseCount): Exercise {
        return generateExercise(count, equationGenerator::generateAdditionEquation)
    }

    /**
     * 根据算式基生成全是加法的习题
     */
    fun generateOfAdditionsByBase(count: Int = this.exerciseCount, base: BinaryEquationBase): Exercise {
        return generateExercise(count, base::getAdditionEquation)
    }

    /**
     * 生成全是减法的习题
     */
    fun generateOfSubtractions(count: Int = this.exerciseCount): Exercise {
        return generateExercise(count, equationGenerator::generateSubtractionEquation)
    }

    /**
     * 根据算式基生成全是减法的习题
     */
    fun generateOfSubtractionByBase(count: Int = this.exerciseCount, base: BinaryEquationBase): Exercise {
        return generateExercise(count, base::getSubtractionEquation)
    }

    /**
     * 生成加减法混合的习题
     */
    fun generateOfRandom(count: Int = this.exerciseCount): Exercise {
        return generateExercise(count, equationGenerator::generateRandomEquation)
    }

    /**
     * 根据算式基生成混合的习题
     */
    fun generateOfRandomByBase(count: Int = this.exerciseCount, base: BinaryEquationBase): Exercise {
        return generateExercise(count, base::getEquation)
    }
}